package com.ltkj.ticket.controller;

import com.ltkj.common.core.domain.R;
import com.ltkj.common.core.utils.Base64Utils;
import com.ltkj.common.core.utils.RSAUtils;
import com.ltkj.common.core.web.controller.BaseController;
import com.ltkj.common.core.web.domain.AjaxResult;
import com.ltkj.common.core.web.page.TableDataInfo;
import com.ltkj.common.security.utils.SecurityUtils;
import com.ltkj.ticket.domain.dto.*;
import com.ltkj.ticket.domain.vo.AppletOrderPageVo;
import com.ltkj.ticket.domain.vo.AppletOrderVenuesPageVo;
import com.ltkj.ticket.domain.vo.AppletTicketPageVo;
import com.ltkj.ticket.service.IAppletTicketService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "小程序——景区、活动门票")
@RestController
@RequestMapping("/appletTicket")
public class ApplectTicketController extends BaseController {

    @Autowired
    private IAppletTicketService appletTicketService;

    @Value("${rsa.publicKey}")
    private String publicKey;
    @Value("${rsa.privateKey}")
    private String privateKey;
    /**
     * 查询票列表
     */
    @ApiOperation("查询票列表")
//    @RequiresPermissions("appletticket:scenic:list")
    @PostMapping("/list")
    public TableDataInfo list(@RequestBody AppletTicketQueryDTO queryDTO)
    {
        startPage();
        List<AppletTicketPageVo> list = appletTicketService.list(queryDTO);
        return getDataTable(list);
    }

    /**
     * 查询票详情
     */
    @ApiOperation("查询票详情")
//    @RequiresPermissions("appletticket:scenic:detail")
    @PostMapping("/detail")
    public AjaxResult detail(@RequestBody AppletTicketDetailQueryDTO queryDTO )
    {
        return AjaxResult.success(appletTicketService.detail(queryDTO));
    }

    /**
     * 查询票列表
     */
    @ApiOperation("查询订单列表")
//    @RequiresPermissions("appletticket:order:list")
    @GetMapping("/listOrder")
    public TableDataInfo listOrder(AppletOrderQueryDTO queryDTO)
    {
        startPage();
        List<AppletOrderPageVo> list = appletTicketService.listOrder(queryDTO);
        return getDataTable(list);
    }

    /**
     * 查询票列表
     */
    @ApiOperation("查询订单列表——体育场")
//    @RequiresPermissions("appletticket:order:list")
    @GetMapping("/listOrderNew")
    public TableDataInfo listOrderNew(AppletOrderNewQueryDTO queryDTO)
    {
        startPage();
        List<AppletOrderPageVo> list = appletTicketService.listOrderNew(queryDTO);
        return getDataTable(list);
    }

    /**
     * 查询票列表
     */
    @ApiOperation("查询订单列表——体育场——场馆")
//    @RequiresPermissions("appletticket:order:list")
    @GetMapping("/listOrderVenues")
    public TableDataInfo listOrderVenues(AppletOrderVenuesQueryDTO queryDTO)
    {
        startPage();
        if(ObjectUtils.isEmpty(queryDTO.getUserId())){
            queryDTO.setUserId(SecurityUtils.getLoginUser().getUserid());
        }

        List<AppletOrderVenuesPageVo> list = appletTicketService.listOrderVenues(queryDTO);
        return getDataTable(list);
    }

    /**
     * 查询票列表
     */
    @ApiOperation("查询订单详情——体育场——场馆")
//    @RequiresPermissions("appletticket:order:detail")
    @GetMapping("/detailVenuesOrder")
    public R<AppletOrderVenuesPageVo> detailVenuesOrder(AppletOrderDetailQueryDTO queryDTO)
    {
        return R.ok(appletTicketService.detailVenuesOrder(queryDTO));
    }

    /**
     * 查询票列表
     */
    @ApiOperation("查询订单详情")
//    @RequiresPermissions("appletticket:order:detail")
    @GetMapping("/detailOrder")
    public AjaxResult listOrder(AppletOrderDetailQueryDTO queryDTO)
    {
        return success(appletTicketService.detailOrder(queryDTO));
    }


    /**
     * 支付成功后修改订单状态
     */
    @ApiOperation("支付成功后修改订单状态")
    @GetMapping("/paySucessStatus/{orderId}")
    public AjaxResult paySucessStatus(@PathVariable("orderId")Long orderId)
    {
        return appletTicketService.paySucessStatus(orderId);
    }

    /**
     * 支付成功后修改订单状态
     */
    @ApiOperation("取消订单状态")
    @GetMapping("/cancleOrder/{orderId}")
    public AjaxResult cancleOrder(@PathVariable("orderId")Long orderId)
    {
        return appletTicketService.cancleOrder(orderId);
    }
    /**
     * 支付成功后修改订单状态
     */
    @ApiOperation("取消订单状态")
    @GetMapping("/cancleVenuesOrder/{orderId}")
    public AjaxResult cancleVenuesOrder(@PathVariable("orderId")Long orderId)
    {
        return appletTicketService.cancleVenuesOrder(orderId);
    }
    /**
     * 票核销修改订单状态
     */
    @ApiOperation("票核销修改订单状态")
    @GetMapping("/ticCheckStatus/{orderNum}")
    public AjaxResult ticCheckStatus(@PathVariable("orderNum")String orderNum)
    {
        return appletTicketService.ticCheckStatus(orderNum);
    }
    @ApiOperation("票核销-对外")
    @GetMapping("/ticCheckStatusForeign")
    public AjaxResult ticCheckStatusForeign(String  orderNumMsg)
    {
        //验证
        try {

            String signKey = RSAUtils.sign(Base64Utils.decode(orderNumMsg), privateKey);
            logger.info("核销票-公钥加密后数字签名："+signKey);
            //校验数字签名
            boolean verify = RSAUtils.verify(Base64Utils.decode(orderNumMsg), publicKey, signKey);
            System.out.println("公钥加密后数字签名校验：" + verify);
            logger.info("核销票-公钥加密后数字签名校验："+verify);
            if(verify){
                String orderNum = new String(RSAUtils.decryptByPrivateKey(Base64Utils.decode(orderNumMsg), privateKey));
                // Long number = Long.valueOf(str);
                //todo需要xiuga

               return appletTicketService.ticCheckStatus(orderNum);
            }else {
                return AjaxResult.error("验签不通过!");
            }
        } catch (Exception e) {
            logger.error("核销票务对外异常"+e.getMessage());
            throw new RuntimeException(e);

        }
    }
    /**
     * 支付成功后修改订单状态
     */
    @ApiOperation("删除订单")
    @GetMapping("/deleteOrder/{orderId}")
    public AjaxResult deleteOrder(@PathVariable("orderId")Long orderId)
    {
        return appletTicketService.deleteOrder(orderId);
    }

    /**
     * 回填快递单号
     */
    @ApiOperation("回填快递单号")
    @GetMapping("/backfill")
    public AjaxResult backfill(Long orderDetailId,String trackingCode)
    {
        return appletTicketService.backfill(orderDetailId,trackingCode);
    }

}
